package com.example.ywj.toutiao;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;

import com.example.ywj.toutiao.fragments.HomeFragment;
import com.example.ywj.toutiao.fragments.MyFragment;
import com.example.ywj.toutiao.fragments.VideoFragment;
import com.example.ywj.toutiao.fragments.XGSPFragment;
import com.example.ywj.toutiao.utils.MyFragmentPagerAdapter;
import com.example.ywj.toutiao.utils.SpUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ArrayList<Fragment> list = new ArrayList<>();
    @BindView(R.id.vp_fragment)
    public ViewPager vp ;
    @BindView(R.id.radio0)
    public RadioButton rbtn1;
    @BindView(R.id.radio1)
    public RadioButton rbtn2;
    @BindView(R.id.radio2)
    public RadioButton rbtn3;
    @BindView(R.id.radio3)
    public RadioButton rbtn4;
    //private SharedPreferences sp ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //sp = getSharedPreferences("user" , Context.MODE_PRIVATE);
        Log.i(TAG, "onCreate: 被执行了");
        ButterKnife.bind(this);
        HomeFragment homeFragment = new HomeFragment();
        XGSPFragment xgspFragment = new XGSPFragment();
        VideoFragment videoFragment = new VideoFragment();
        MyFragment myFragment = new MyFragment();
        list.add(homeFragment);
        list.add(xgspFragment);
        list.add(videoFragment);
        list.add(myFragment);
        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),list);

        vp.setAdapter(myFragmentPagerAdapter);
        vp.setOffscreenPageLimit(4);

        boolean isFirstOpen = SpUtils.getBoolean(this, AppConstants.FIRST_OPEN);
        boolean isReturn = SpUtils.getBoolean(MainActivity.this,"isreturn");
        //Log.d(TAG, "onCreate: " + isFirstOpen);
        // 如果是第一次启动，则先进入功能引导页
        if (!isFirstOpen) {
            Intent intent = new Intent(this, AppIntroActivity.class);
            startActivity(intent);
            finish();
            return;
        }else if(isFirstOpen && isReturn){
            Intent intent = new Intent(MainActivity.this,IndexActivity.class);
            startActivity(intent);
            finish();
            //SpUtils.putBoolean(MainActivity.this,"isreturn",true);
            return;
        }

    }
    @OnClick({R.id.radio0,R.id.radio1,R.id.radio2,R.id.radio3})
    public void onCheckedChanged(RadioButton radioButton){
        boolean checked = radioButton.isChecked();
        switch (radioButton.getId()) {
            case R.id.radio0:
                if (checked) {
                    vp.setCurrentItem(0);
                }
                break;
            case R.id.radio1:
                if (checked) {
                    vp.setCurrentItem(1);
                }
                break;
            case R.id.radio2:
                if (checked) {
                    vp.setCurrentItem(2);
                }
                break;
            case R.id.radio3:
                if (checked) {
                    vp.setCurrentItem(3);
                }
                break;
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
        SpUtils.putBoolean(MainActivity.this,"isreturn",true);
        Log.i(TAG, "onPause: 被执行了");
    }



    public class AppConstants {
        public static final String FIRST_OPEN = "first_open";
    }

    //@Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }
}
